package com.itheima.controller;

import com.itheima.bean.Employee;
import com.itheima.bean.PageBean;
import com.itheima.bean.PageParams;
import com.itheima.common.R;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


/**
 * @author hebing
 * @version 1.0
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService es;

    //登录
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session) {

        //1.调用service的方法查询数据库返回对象
        System.out.println(employee);
        Employee loginEmployee = es.employeeLogin(employee);
        //2.判断
        if (loginEmployee == null) {
            //不存在
            return R.error("登录失败!用户名或密码不存在");
        } else {
            //存在
            //将对象保存到session对象中
            session.setAttribute("employee", loginEmployee.getId());
            //3.返回
            return R.success(loginEmployee);
        }
    }

    //点击退出清空session对象中的值
    @PostMapping("/logout")
    public R logout(HttpSession session) {
        //清空session作用域
        session.invalidate();
        return R.success("退出成功");
    }

    //点击添加按钮 进行新增员工
    @PostMapping("/add")
    public R<String> add(@RequestBody Employee employee, HttpServletRequest request) {
        //给定一个默认的md5加密密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置时间 创建时间  修改时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //获取当前登录用户的id
        //Long empId = (long) request.getSession().getAttribute("employee");
        //创建者
        //employee.setCreateUser(empId);
        //修改者
        //employee.setUpdateUser(empId);
        //设置性别
        if (employee.getSex().equals("女")) {
            employee.setSex("0");
        } else {
            employee.setSex("1");
        }
        //修改者id
        int rows = es.add(employee);
        if (rows > 0) {
            return R.success("添加员工成功");
        } else {
            return R.error("添加员工失败");
        }
    }

    //分页查询
    @GetMapping("/page")
    public R<PageBean<Employee>> findPage(PageParams params) {
        //调用方法查询数据库
        PageBean<Employee> pageBean = es.findPage(params);
        //判断
        if (null != pageBean) {
            return R.success(pageBean);
        } else {
            return R.error("查询失败");
        }

    }

    //根据id修改员工信息
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());
        //调用service层方法操作数据库进行修改
        int rows = es.update(request, employee);
        if (rows > 0) {
            return R.success("员工信息修改成功");
        } else {
            return R.error("员工信息修改失败");
        }
    }

    //根据id查询员工信息
    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable Long id) {
        //调用service层查询数据库
        Employee employee = es.selectById(id);
        if(employee!=null){
            return R.success(employee);
        }else {
            return R.error("查询有误");
        }
    }

    //保存员工信息
    @PostMapping("/addUpdate")
    public R<String> addUpdate(@RequestBody Employee employee,HttpServletRequest request){
        log.info(employee.toString());
        //从session中获取修改人员的id
        //Long empId = (Long) request.getSession().getAttribute("employee");
        //设置修改员工时间和修改人员
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(empId);
        //调用service保存信息
        int rows=es.addUpdate(employee);
        if(rows>0){
            return R.success("修改成功");
        }else{
            return R.error("修改失败");
        }
    }

}

